package cn.dinkevin.xui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import cn.dinkevin.xui.R;
import cn.dinkevin.xui.finder.ValueFinder;
import cn.dinkevin.xui.finder.ViewFinder;
import cn.dinkevin.xui.thread.ThreadExecutor;
import cn.dinkevin.xui.util.StringUtil;
import cn.dinkevin.xui.widget.progress.HorizontalProgressBar;

/**
 * 进度条对话框
 * Created by chengpengfei on 2017/5/7.
 * Copyright ©2017 juziwl, All Rights Reserved.
 */

public class ProgressDialog {

    private Context context;
    private boolean showCancel = true ,showOK = true,showProgress = true,cancelable = true, touchOutCancelable = true;     // 是否展示取消，是否可以取消，是否点击外部自动取消
    private String title,content;           // 标题、内容
    private String cancelText, okText;     // 取消按钮文字显示、确定按钮的文字显示
    private int cancelTextColor, okTextColor;        // 取消按钮文字颜色、确定按纽的文字颜色
    private final int MAX_PROGRESS = 100;   // 最大进度
    private final int MIN_PROGRESS = 0;     // 最小进度
    private int progress = 0;               // 当前进度
    private boolean dialogIsShowing = false;     // 对话框是否可见

    public ProgressDialog(Context context){
        this.context = context;
    }

    public void setTitle(String title){
        this.title = title;
        initialView();
    }

    public void setContent(String content){
        this.content = content;
        initialView();
    }

    public void setShowCancel(boolean showCancel){
        this.showCancel = showCancel;
        initialView();
    }

    public void setShowOK(boolean showOK){
        this.showOK = showOK;
        initialView();
    }

    public void setCancelable(boolean cancelable){
        this.cancelable = cancelable;
        initialView();
    }

    public void setTouchOutCancelable(boolean touchOutCancelable){
        this.touchOutCancelable = touchOutCancelable;
        initialView();
    }

    public void setCancelTextColor(int cancelTextColor){
        this.cancelTextColor = cancelTextColor;
        initialView();
    }

    public void setOkTextColor(int okTextColor){
        this.okTextColor = okTextColor;
        initialView();
    }

    public void setCancelText(String cancelText){
        this.cancelText = cancelText;
        initialView();
    }

    public void setOKText(String okText){
        this.okText = okText;
        initialView();
    }

    public void setShowProgress(boolean showProgress){
        this.showProgress = showProgress;
        initialView();
    }

    public void setProgress(int progress){
        if(progress > MAX_PROGRESS || progress < MIN_PROGRESS) return;
        this.progress = progress;
        initialView();
    }

    // 控件
    private TextView txt_cancel,txt_ok,txt_title,txt_content;
    private Dialog confirmDialog;
    private HorizontalProgressBar prb_load;

    private void initialView(){
        if(!dialogIsShowing) return;
        ThreadExecutor.main(() -> {
            // 标题
            txt_title.setText(title);
            txt_title.setVisibility(StringUtil.isEmpty(title) ? View.GONE : View.VISIBLE);

            // 内容
            txt_content.setText(content);
            txt_content.setVisibility(StringUtil.isEmpty(content) ? View.GONE : View.VISIBLE);
            txt_ok.setVisibility(showOK ? View.VISIBLE : View.GONE);
            txt_ok.setOnClickListener(v -> {
                confirmDialog.cancel();
                if(resultListener != null)
                    resultListener.onConfirmResult(true);
            });

            if(okTextColor > 0)
                txt_ok.setTextColor(okTextColor);

            if(!StringUtil.isEmpty(okText))
                txt_ok.setText(okText);

            // 判断是否显示取消按钮
            txt_cancel.setVisibility(showCancel ? View.VISIBLE : View.GONE);
            if(showCancel){
                txt_cancel.setOnClickListener(v -> {
                    confirmDialog.cancel();
                    if(resultListener != null)
                        resultListener.onConfirmResult(false);
                });

                if(cancelTextColor > 0)
                    txt_cancel.setTextColor(cancelTextColor);

                if(!StringUtil.isEmpty(cancelText))
                    txt_cancel.setText(cancelText);
            }

            prb_load.setVisibility(showProgress ? View.VISIBLE : View.GONE);
            prb_load.setProgressValue(progress);
        });
    }

    /**
     * 确认对话框回调
     */
    public interface DialogResultListener {

        /**
         * 确认结果回调
         * @param isOk 是否选择确定
         */
        void onConfirmResult(boolean isOk);
    }

    private ProgressDialog.DialogResultListener resultListener;

    public void setResultListener(ProgressDialog.DialogResultListener resultListener){
        this.resultListener = resultListener;
    }

    private DialogInterface.OnDismissListener dismissListener;
    public void setDismissListener(DialogInterface.OnDismissListener dismissListener){
        this.dismissListener = dismissListener;
    }

    public Dialog show(){
        // try to close dialog
        close();

        LayoutInflater inflater = LayoutInflater.from(context);
        View contentView = inflater.inflate(R.layout.xui_progress_dialog, null);
        confirmDialog = new Dialog(context,R.style.TransDialogStyle);    // 创建自定义样式dialog
        confirmDialog.setContentView(contentView);
        confirmDialog.setCancelable(cancelable);
        confirmDialog.setCanceledOnTouchOutside(touchOutCancelable);

        ViewFinder finder = new ViewFinder(contentView);
        txt_title = finder.findViewById(R.id.txt_title);
        txt_content = finder.findViewById(R.id.txt_content);
        txt_ok = finder.findViewById(R.id.txt_ok);
        txt_cancel = finder.findViewById(R.id.txt_cancel);
        prb_load = finder.findViewById(R.id.prb_load);
        prb_load.setMaxProgress(MAX_PROGRESS);
        prb_load.setNormalColor(ValueFinder.getColor(R.color.xui_line));

        Window window = confirmDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        confirmDialog.setOnDismissListener(dialogInterface -> {
            dialogIsShowing = false;
            if(null != dismissListener){
                dismissListener.onDismiss(dialogInterface);
            }
        });
        confirmDialog.show();
        dialogIsShowing = true;
        initialView();
        return confirmDialog;
    }

    public void close(){
        if(null != confirmDialog){
            confirmDialog.cancel();
            confirmDialog.dismiss();
            confirmDialog = null;
        }
    }

    /**
     * Builder
     */
    public static class Builder{

        private ProgressDialog dialog;

        public Builder(Context context){
            dialog = new ProgressDialog(context);
        }

        public ProgressDialog.Builder setTitle(String title){
            dialog.setTitle(title);
            return this;
        }

        public ProgressDialog.Builder setContent(String content){
            dialog.setContent(content);
            return this;
        }

        public ProgressDialog.Builder setShowCancel(boolean showCancel){
            dialog.setShowCancel(showCancel);
            return this;
        }

        public ProgressDialog.Builder setShowOK(boolean showOK){
            dialog.setShowOK(showOK);
            return this;
        }

        public ProgressDialog.Builder setCancelable(boolean cancelable){
            dialog.setCancelable(cancelable);
            return this;
        }

        public ProgressDialog.Builder setTouchOutCancelable(boolean touchOutCancelable){
            dialog.setTouchOutCancelable(touchOutCancelable);
            return this;
        }

        public ProgressDialog.Builder setCancelTextColor(int cancelTextColor){
            dialog.setCancelTextColor(cancelTextColor);
            return this;
        }

        public ProgressDialog.Builder setOkTextColor(int okTextColor){
            dialog.setOkTextColor(okTextColor);
            return this;
        }

        public ProgressDialog.Builder setCancelText(String cancelText){
            dialog.setCancelText(cancelText);
            return this;
        }

        public ProgressDialog.Builder setOKText(String okText){
            dialog.setOKText(okText);
            return this;
        }

        public ProgressDialog.Builder setShowProgress(boolean showProgress){
            dialog.setShowProgress(showProgress);
            return this;
        }

        public ProgressDialog.Builder setProgress(int progress){
            dialog.setProgress(progress);
            return this;
        }

        public ProgressDialog.Builder setResultListener(ProgressDialog.DialogResultListener resultListener){
            dialog.setResultListener(resultListener);
            return this;
        }

        public ProgressDialog.Builder setDismissListener(DialogInterface.OnDismissListener dismissListener){
            dialog.setDismissListener(dismissListener);
            return this;
        }

        public ProgressDialog build(){
            return dialog;
        }
    }
}
